package com.bydq.patrolsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bydq.patrolsystem.entity.PsLineInfo;
import com.bydq.patrolsystem.entity.PsPoleInfo;
import com.bydq.patrolsystem.entity.PsUser;
import com.bydq.patrolsystem.service.PsLineInfoService;
import com.bydq.patrolsystem.service.PsPoleInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/pole")
public class PsPoleInfoController {

    @Autowired
    private PsPoleInfoService psPoleInfoService;

    @Autowired
    private PsLineInfoService psLineInfoService;

    /**
     * 查询杆塔管理列表页
     * @param m
     * @return
     */
    @ResponseBody
    @RequestMapping("/list")
    public Map<String,Object> poleList(Long lineId, Integer isStart,Integer page,Integer limit){
        page = page == null ? 1 : page;
        limit = limit == null ? 3 : limit;
        System.out.println("lineId:"+lineId);
        System.out.println("isStart:"+isStart);
        IPage<PsPoleInfo> pages = new Page<>(page,limit);
        QueryWrapper<PsPoleInfo> qw=new QueryWrapper<>();
        qw.eq(lineId!=null,"lineId",lineId);
        qw.eq(isStart!=null,"isStart",isStart);
        psPoleInfoService.page(pages,qw);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",pages.getTotal());
        map.put("data",pages.getRecords());
        System.out.println(map.toString());

        return map;
    }

    /**
     * 页面跳转至pole.xml
     * @return
     */
    @RequestMapping("/manage")
    public String poleManage( ){
            return "pole";
    }

    /**
     * 页面跳转至pole_add.xml
     * @return
     */
    @RequestMapping("/pole_add")
    public String pole_add(Integer lineId,Model m){
        m.addAttribute("lineId",lineId);
        return "pole_add.html";
    }

    /**
     * 页面跳转至pole_edit.xml
     * @return
     */
    @RequestMapping("/pole_edit")
    public String pole_eidt(Integer id,Model m){
        System.out.println("id:"+id);
        if(id!=null){
            //修改
            m.addAttribute("psPoleInfo",psPoleInfoService.getById(id));
        }
        return "pole_edit";
    }

    /**
     * 查询线路列表集合
     * @param
     * @return
     */
    @RequestMapping("/lineList")
    @ResponseBody
    public List<PsLineInfo> lineList(){

        return psLineInfoService.list();
    }


    /**
     * 添加修改杆塔
     * @param
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public boolean save(PsPoleInfo pole, HttpSession session){
        System.out.println("PoleCode:"+pole.getPoleCode());
        System.out.println("getIsStart:"+pole.getIsStart());
        boolean flag = false;
        if(pole.getId()!=null){
            System.out.println("修改");
            flag = psPoleInfoService.updateById(pole);
        }else{
            System.out.println("添加");
            PsLineInfo psLineInfo = psLineInfoService.getById(pole.getLineId());
            pole.setLineName(psLineInfo.getLineName());
            pole.setLineCode(psLineInfo.getLineCode());
            PsUser user = (PsUser)session.getAttribute("user");
            pole.setCreatedBy(user.getUserName());
            flag = psPoleInfoService.save(pole);
        }
        return flag;
    }

    /**
     * 验证杆塔编号是否存在,存在返回false，不存在返回true
     * @param
     * @return
     */
    @RequestMapping("/seclectByPoleCode")
    @ResponseBody
    public boolean seclectByPoleCode(@RequestBody PsPoleInfo pole) {
        System.out.println("poleCode:"+pole.getPoleCode());
        boolean flag = psPoleInfoService.countByPoleCode(pole.getPoleCode());
        System.out.println("flag:"+flag);
        return flag;

    }

    /**
     * 删除杆塔
     * @param
     * @return
     */
    @RequestMapping("/del")
    @ResponseBody
    public boolean del(Integer id) {
        System.out.println("id:"+id);
        boolean flag = psPoleInfoService.removeById(id);
        System.out.println("flag:"+flag);
        return flag;

    }




}
